/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * URIResourceIDDialog.java
 *
 * Created on 16-mag-2011, 12.49.55
 */
package it.txt.access.capability.spi.resource.txt.dialog;

import it.txt.access.capability.commons.filter.FilterTXT;
import it.txt.access.capability.spi.resource.txt.model.TXTResourceIDModel;
import it.txt.access.capability.spi.resource.model.ICapabilityResourceIDModel;
import it.txt.access.capability.spi.resource.visual.ICapabilityResourceIDDialog;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author seccia
 */
public class TXTResourceIDDialog extends ICapabilityResourceIDDialog {

    private JFileChooser txtFileChooser = new JFileChooser();
    private FilterTXT filterResource = new FilterTXT();
    private File lastSelectedTXTFile = null;
    
    /** Creates new form URIResourceIDDialog */
    public TXTResourceIDDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setTitle("Capability :: Plugin :: Resource :: TXT");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        txtFileNameLabel = new javax.swing.JLabel();
        txtFileNameButton = new javax.swing.JButton();
        txtFileNameText = new javax.swing.JTextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("URI ResourceID"));

        txtFileNameLabel.setText("Path");

        txtFileNameButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Open_16x16.png"))); // NOI18N
        txtFileNameButton.setText("Open");
        txtFileNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFileNameButtonActionPerformed(evt);
            }
        });

        txtFileNameText.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtFileNameLabel)
                .addGap(18, 18, 18)
                .addComponent(txtFileNameText, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(txtFileNameButton))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFileNameLabel)
                    .addComponent(txtFileNameButton)
                    .addComponent(txtFileNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void txtFileNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFileNameButtonActionPerformed
        if (lastSelectedTXTFile == null) {
            txtFileChooser.setCurrentDirectory(new File("."));
        }
        else {
            txtFileChooser.setCurrentDirectory(lastSelectedTXTFile);
        }
        txtFileChooser.setFileFilter(filterResource);
        txtFileChooser.setMultiSelectionEnabled(false);
        int returnValue = txtFileChooser.showOpenDialog(getRootPane());
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File resourceFile = txtFileChooser.getSelectedFile();
            lastSelectedTXTFile = resourceFile;
            this.txtFileNameText.setText(resourceFile.getPath());
        }
    }//GEN-LAST:event_txtFileNameButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton okButton;
    private javax.swing.JButton txtFileNameButton;
    private javax.swing.JLabel txtFileNameLabel;
    private javax.swing.JTextField txtFileNameText;
    // End of variables declaration//GEN-END:variables

    @Override
    public ICapabilityResourceIDModel getModel() {
        TXTResourceIDModel iDModel = new TXTResourceIDModel();
        iDModel.setTxtFile(lastSelectedTXTFile);
        return iDModel;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TXTResourceIDDialog dialog = new TXTResourceIDDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
}
